package com.centling.smartSealForPhone.activity

import android.content.Intent
import android.os.Bundle
import android.os.Parcelable
import android.text.TextUtils
import android.view.View
import com.centling.smartSealForPhone.R
import com.centling.smartSealForPhone.bean.DetailBean
import com.centling.smartSealForPhone.constants.UrlConstants
import com.centling.smartSealForPhone.utils.ImageUtil
import com.centling.smartSealForPhone.utils.OkHttpUtil
import com.centling.smartSealForPhone.utils.ShowToast
import com.example.localalbum.common.LocalImageHelper
import com.example.localalbum.ui.PagerView
import com.google.gson.Gson
import kotlinx.android.synthetic.main.activity_history_details.*
import java.io.Serializable
import java.util.*

class HistoryDetailsActivity : TitleBarActivity() {

    private val userList = ArrayList<String>()
    private val fileList = ArrayList<String>()

    private val list = ArrayList<DetailBean.OperatorSealTimesEntity>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_history_details)
        setTitleBarText("历史记录详情")
        initData()
        sendRequestToFetchInfo()
    }

    private fun initData() {
//        rv_history_detail_user_time.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
//        rv_history_detail_user_time.adapter = RvUserTimeAdapter(this, list)
    }

    private fun sendRequestToFetchInfo() {
        mProcessDialog.setTitle("正在获取详情").showNormal()
        getEnqueue(String.format(Locale.CHINA, UrlConstants.DETAIL_NEW, intent.getStringExtra("sealApplyId")), object : OkHttpUtil.NetCallBack {
            override fun onSucceed(json: String?) {
                mProcessDialog.dismiss()
                val bean = Gson().fromJson(json, DetailBean::class.java)
                initStatus(bean)
            }

            override fun onFailed(reason: String?) {
                mProcessDialog.dismiss()
                ShowToast.shortTime(reason)
                finish()
            }
        })
    }

    private fun initStatus(bean: DetailBean) {
        if (bean.isValid == "0") {
            tv_history_detail_is_valid.text = "正常"
        } else {
            tv_history_detail_is_valid.text = "换章失效"
        }

        tv_history_detail_user.text = bean.applicantUserName
        tv_history_detail_seal_reg.text = bean.sealReg
        tv_history_detail_apply_people.text = bean.applyOperator
        tv_history_detail_apply_reason.text = "${bean.applyReasonType}${if (TextUtils.isEmpty(bean.applyReason)) "" else ":" }${bean.applyReason}"

        when (bean.applyType) {
            "TIME_LIMIT" -> {
                ll_apply_type_time.visibility = View.VISIBLE
                tv_apply_time_end.text = bean.applyEndTimeReal
                tv_apply_time_start.text = bean.applyStartTimeReal
            }
            "COUNT_LIMIT" -> {
                ll_apply_type_count.visibility = View.VISIBLE
                tv_history_detail_apply_time.text = bean.applyDate

                /**
                 * stamp pics
                 */
                val stampPics = bean.sealPic.split(",".toRegex()).dropLastWhile(String::isEmpty).toTypedArray()
                val pics = ArrayList<String>()
                for (i in stampPics.indices) {
                    pics.add(stampPics[i])
                }
                stamp_scroll_view.addFiles(pics)
            }
        }

        tv_history_detail_approval_history.setOnClickListener {
            if (bean.sealApplyAuditRecordList != null && !bean.sealApplyAuditRecordList.isEmpty()) {
                startActivity(Intent(mContext, HistoryApprovalRecordsActivity::class.java).putParcelableArrayListExtra("history_stamp_records", bean.sealApplyAuditRecordList as ArrayList<out Parcelable>))
            } else {
                ShowToast.shortTime("没有审阅记录")
            }
        }

        userList.add(bean.applicantUserAvatar)
        ImageUtil.loadImage(userList[0], user_scroll_view, R.drawable.iv_avatar_default)
        user_scroll_view.setOnClickListener {
            val pictures: MutableList<LocalImageHelper.LocalFile> = ArrayList()
            val file = LocalImageHelper.LocalFile()
            file.originalUri = userList[0]
            file.thumbnailUri = userList[0]
            pictures.add(file)
            val intent = Intent(mContext, PagerView::class.java)
            intent.putExtra("FILE_LIST", pictures as Serializable)
            intent.putExtra("INDEX", 0)
            mContext.startActivity(intent)
        }

        val files = bean.docPics.split(",".toRegex()).dropLastWhile(String::isEmpty).toTypedArray()
        for (i in files.indices) {
            fileList.add(files[i])
        }
        file_scroll_view.addFiles(fileList)

        when (bean.status) {
            "", "0" -> {
                tv_history_detail_state.text = "审批中"
            }
            "1" -> {
                tv_history_detail_state.text = "用印中"
                ll_history_detail_authorize_time.visibility = View.VISIBLE
                tv_history_detail_authorize_time.text = bean.authTimeReal
            }
            "2" -> {
                tv_history_detail_state.text = "审批失败"
                tv_history_detail_failed_reason_title.visibility = View.VISIBLE
                tv_history_detail_failed_reason.visibility = View.VISIBLE
                tv_history_detail_failed_reason.text = bean.authFailReason
            }
            "3" -> {
                tv_history_detail_state.text = "未审批过期"
                ll_history_detail_authorize_time.visibility = View.VISIBLE
                tv_history_detail_authorize_time_title.setAlignText("过期时间:")
                tv_history_detail_authorize_time.text = bean.expireTimeReal
            }
            "4" -> {
                tv_history_detail_state.text = "审批后过期"
                ll_history_detail_authorize_time.visibility = View.VISIBLE
                tv_history_detail_authorize_time_title.setAlignText("过期时间:")
                tv_history_detail_authorize_time.text = bean.expireTimeReal
            }
            "5", "6" -> {
                ll_history_detail_authorize_time.visibility = View.VISIBLE
                tv_history_detail_authorize_time.text = bean.authTimeReal
                if (bean.operatorSealTimes != null && !bean.operatorSealTimes.isEmpty()) {
                    list.addAll(bean.operatorSealTimes)
//                    rv_history_detail_user_time.adapter.notifyDataSetChanged()
                    tv_history_detail_title_user_and_time.visibility = View.VISIBLE
                    tv_history_detail_title_user_and_time.setOnClickListener {
                        startActivity(Intent(mContext, HistoryStampRecordsActivity::class.java).putParcelableArrayListExtra("history_stamp_records", bean.operatorSealTimes as ArrayList<out Parcelable>))
                    }
//                    rv_history_detail_user_time.visibility = View.VISIBLE
                    ll_use_device_history_pic.visibility = View.VISIBLE
                }
                when (bean.status) {
                    "5" -> {
                        tv_history_detail_state.text = "用章完成"
                    }
                    "6" -> {
                        tv_history_detail_state.text = "复审成功"
                    }
                }
            }
            "7" -> {
                tv_history_detail_state.text = "复审失败"
                tv_history_detail_title_user_and_time.visibility = View.VISIBLE
//                rv_history_detail_user_time.visibility = View.VISIBLE
                list.addAll(bean.operatorSealTimes)
//                rv_history_detail_user_time.adapter.notifyDataSetChanged()

                tv_history_detail_title_user_and_time.visibility = View.VISIBLE
                tv_history_detail_title_user_and_time.setOnClickListener {
                    startActivity(Intent(mContext, HistoryStampRecordsActivity::class.java).putParcelableArrayListExtra("history_stamp_records", bean.operatorSealTimes as ArrayList<out Parcelable>))
                }

                ll_history_detail_review_false_time.visibility = View.VISIBLE
                tv_history_detail_review_false_time.text = bean.reviewcreatetimeReal

//                stamp_scroll_view.files.clear()
//                val userAndPics = bean.pics.split(",".toRegex()).dropLastWhile(String::isEmpty).toTypedArray()
//                val pictures = ArrayList<String>()
//                val users = ArrayList<String>()
//                for (userAndPic in userAndPics) {
//                    val pic = userAndPic.split("|".toRegex()).dropLastWhile(String::isEmpty).toTypedArray()
//                    if (pic.size == 2) {
//                        pictures.add(UrlConstants.ROOTURL + "/" + pic[0])
//                        users.add(pic[1])
//                    }
//                }
//                stamp_scroll_view.addFiles(pictures, users)
                ll_history_detail_review_state.visibility = View.VISIBLE
                tv_history_detail_review_state.text = if (TextUtils.equals(bean.reviewResultType,"RESHOOT")) "补拍" else "非法用印"
                ll_use_device_history_pic.visibility = View.VISIBLE
                tv_history_detail_failed_reason_title.visibility = View.VISIBLE
                tv_history_detail_failed_reason.visibility = View.VISIBLE
                tv_history_detail_failed_reason.text = bean.reviewFailReason
            }
            "8" -> {
                ll_use_device_history_pic.visibility = View.VISIBLE

                tv_history_detail_state.text = "补拍完成"
                tv_history_detail_title_user_and_time.visibility = View.VISIBLE
//                rv_history_detail_user_time.visibility = View.VISIBLE
                list.addAll(bean.operatorSealTimes)
//                rv_history_detail_user_time.adapter.notifyDataSetChanged()
                tv_history_detail_title_user_and_time.visibility = View.VISIBLE
                tv_history_detail_title_user_and_time.setOnClickListener {
                    startActivity(Intent(mContext, HistoryStampRecordsActivity::class.java).putParcelableArrayListExtra("history_stamp_records", bean.operatorSealTimes as ArrayList<out Parcelable>))
                }

                ll_history_detail_authorize_time.visibility = View.VISIBLE
                tv_history_detail_authorize_time.text = bean.authTimeReal
            }
            "9" -> {
                tv_history_detail_state.text = "申请驳回"
                tv_history_detail_failed_reason_title.visibility = View.VISIBLE
                tv_history_detail_failed_reason_title.text = "驳回原因"
                tv_history_detail_failed_reason.visibility = View.VISIBLE
                tv_history_detail_failed_reason.text = bean.authFailReason
            }
            "10" -> {
                tv_history_detail_state.text = "驳回过期"
                tv_history_detail_failed_reason_title.visibility = View.VISIBLE
                tv_history_detail_failed_reason_title.text = "驳回原因"
                tv_history_detail_failed_reason.visibility = View.VISIBLE
                tv_history_detail_failed_reason.text = bean.authFailReason
            }
        }

    }
}